﻿using System;
using System.Collections;
using System.Collections.Generic;
using Const;
using Entitas;
using UnityEngine;

public class ChangeItemSpriteSystem : ReactiveSystem<GameEntity>
{
    public ChangeItemSpriteSystem(Contexts context) : base(context.game)
    {
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsItemEffectState);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsItemEffectState
               && entity.componentsItemEffectState.EffectState != ItemEffectName.NONE;
    }

    protected override void Execute(List<GameEntity> entities)
    {
       
        foreach (GameEntity entity in entities)
        {
            string[] buffer = entity.componentsLoadPrefab.Path.Split('/');
            string itemName = "";
            if (buffer.Length == 2)
            {
                itemName = buffer[1];
            }
            else
            {
                continue;
            }

            switch (entity.componentsItemEffectState.EffectState)
            {
                case ItemEffectName.ELIMINATE_SAME_COLOR:
                    itemName = ResPath.AllFix;
                    break;
                case ItemEffectName.ELIMINATE_HORIZONTAL:
                    itemName += ResPath.Horizontal;
                    break;
                case ItemEffectName.ELIMINATE_VERTICAL:
                    itemName += ResPath.Vertical;
                    break;
                case ItemEffectName.EXPLODE:
                    itemName += ResPath.Explode;
                    break;
            }
            
            //Debug.Log(itemName);
            entity.ReplaceComponentsLoadSprite(itemName);
            
        }
    }

   
}
